home *** CD-ROM | disk | FTP | other *** search
/ Loadstar 37 / 037.d81 / better bubble (.txt) < prev    next >
Commodore BASIC  |  2022-08-26  |  3KB  |  127 lines

  1. 10 gosub60100:clr
  2. 20 :
  3. 30 n=50:       :rem  number of elements
  4. 40 dim a(n):   :rem  array of random
  5. 50 :
  6. 80 na$(1)="standard"
  7. 81 na$(2)="flags"
  8. 82 na$(3)="wrinkle"
  9. 83 na$(4)="simple"
  10. 84 na$(5)="m-search"
  11. 90 goto5000
  12. 97 :
  13. 98 :        standard bubble sort
  14. 99 :
  15. 100 fori=n-1to1step-1
  16. 110 forj=1toi
  17. 120 ifa(j)<=a(j+1)then140
  18. 130 t=a(j):a(j)=a(j+1):a(j+1)=t
  19. 140 nextj
  20. 150 nexti
  21. 160 return
  22. 197 :
  23. 198 :        flags bubble sort
  24. 199 :
  25. 200 fori=n-1to1step-1
  26. 205 f=1
  27. 210 forj=1toi
  28. 220 ifa(j)<=a(j+1)then240
  29. 225 f=0
  30. 230 t=a(j):a(j)=a(j+1):a(j+1)=t
  31. 240 nextj
  32. 245 iffthen260
  33. 250 nexti
  34. 260 return
  35. 297 :
  36. 298 :      wrinkle bubble sort
  37. 299 :
  38. 300 fori=1ton-1
  39. 310 ifa(i)<=a(i+1)then360
  40. 320 forj=ito1step-1
  41. 330 ifa(j)<=a(j+1)then360
  42. 340 t=a(j):a(j)=a(j+1):a(j+1)=t
  43. 350 nextj
  44. 360 nexti
  45. 370 return
  46. 397 :
  47. 398 :       simple bubble sort
  48. 399 :
  49. 400 fori=1ton-1
  50. 410 forj=i+1ton
  51. 420 ifa(i)<=a(j)then440
  52. 430 t=a(i):a(i)=a(j):a(j)=t
  53. 440 nextj
  54. 450 nexti
  55. 460 return
  56. 497 :
  57. 498 :     m-search bubble sort
  58. 499 :
  59. 500 fori=1ton-1:m=i
  60. 510 forj=i+1ton
  61. 520 ifa(m)>a(j)thenm=j
  62. 530 nextj
  63. 540 t=a(i):a(i)=a(m):a(m)=t
  64. 550 nexti
  65. 560 return
  66. 598 :
  67. 599 :
  68. 1000 tm=ti/60:rem  finishing time
  69. 1010 fori=1ton
  70. 1020 printa(i),:next:print
  71. 1030 print"elapsed time ="tm"seconds"
  72. 1040 return
  73. 5000 print"[147]         making a better bubble"
  74. 5010 print"   this program will demonstrate five"
  75. 5020 print"bubble sort routines."
  76. 5030 print"     press space to start demo."
  77. 5040 ifpeek(203)<>60then5040
  78. 5050 for loop=1 to 5
  79. 5060 ti$="000000":i=rnd(-1000):pr$=na$(loop)+" bubble sort"
  80. 5070 ta=(40-len(pr$))/2:print"[147]"tab(ta)pr$""
  81. 5080 fori=1ton
  82. 5090 a(i)=int(rnd(1)*n)
  83. 5100 printa(i),:next
  84. 5105 print:print"         sorting"n"numbers..."
  85. 5110 on loop gosub 100,200,300,400,500
  86. 5115 print"[147]"tab(ta)pr$""
  87. 5116 fori=1ton
  88. 5117 printa(i),:next
  89. 5120 tm(loop)=ti/60:print:print"it took"tm(loop)"seconds to sort"
  90. 5130 print"using the "pr$" routine."
  91. 5140 print"       press space to continue."
  92. 5150 ifpeek(203)<>60then5150
  93. 5160 next
  94. 5170 print"[147]         making a better bubble"
  95. 5180 print"            summary of demos"
  96. 5190 print"type of sort"tab(20)"time"
  97. 5200 print"[192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192]"
  98. 5210 for loop=1 to 5
  99. 5220 printna$(loop)tab(20)tm(loop)
  100. 5230 next
  101. 5240 print"[192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192]"
  102. 5250 print"press space to see demo again; press"
  103. 5260 print"return to return to loadstar."
  104. 5270 a=peek(203):ifa<>60anda<>1then5270
  105. 5280 ifa=60thenrun30
  106. 5290 open15,8,15,"r0:hello connect=hello connect":input#15,er:close15
  107. 5300 ifer<>63thenend
  108. 5310 load"hello connect",8
  109. 59999 stop
  110. 60100 poke53280,14:poke53281,14
  111. 60110 printchr$(142)"[147][213][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][201]";
  112. 60120 fora=1to23:print"[221]"spc(38)"[221]";:next
  113. 60130 print"[202][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][192][203][157][148][192]"
  114. 60140 fora=217to242:pokea,peek(a)or128:next
  115. 60150 print"          loadstar presents:"
  116. 60160 print"                                "
  117. 60170 print"        making a better bubble   "
  118. 60180 print"                                 "
  119. 60190 print"          by royal c. jones      "
  120. 60200 print"                                 "
  121. 60210 print"            copyright 1987       "
  122. 60220 print"                                 "
  123. 60230 print"                                 "
  124. 60240 print"        press space to continue."
  125. 60250 ifpeek(203)<>60then60250
  126. 60260 return
  127.